package com.spark.common.query;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.spark.common.annotation.query.Query;
import com.spark.common.constants.QueryConstants;
import com.spark.common.utils.StringUtils;
import lombok.extern.slf4j.Slf4j;

import java.lang.reflect.Field;

/**
 * 基于Mybatis-Plus简单条件构造器
 *
 * @author LYCHEE
 * @date 2025/2/14 16:15
 */
@Slf4j
public class QueryGenerator {

    /**
     * 获取LambdaQueryWrapper
     * @param entity 实体
     * @return LambdaQueryWrapper
     * @param <T> 泛型
     */
    public static <T> LambdaQueryWrapper<T> getLambdaQuery(T entity){
        return new LambdaQueryWrapper<T>();
    }

    /**
     * 构造QueryWrapper
     * @param entity 需要构造的实体
     * @return QueryWrapper
     * @param <T> 泛型
     */
    public static <T> QueryWrapper<T> initQueryWrapper(T entity){
        QueryWrapper<T> queryWrapper = new QueryWrapper<>();
        installConditions(queryWrapper, entity);
        return queryWrapper;
    }

    /**
     * 构造查询条件
     * @param queryWrapper 条件构造器
     * @param entity 实体
     * @param <T> 泛型
     */
    private static <T> void installConditions(QueryWrapper<T> queryWrapper, T entity) {
        Class<?> clazz = entity.getClass();
        Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            // 检查字段是否存在 Query 注解
            if (field.isAnnotationPresent(Query.class) && field.getAnnotation(Query.class).value() != null) {
                Query query = field.getAnnotation(Query.class);
                field.setAccessible(true);
                try {
                    // 获取字段值
                    Object value = field.get(entity);
                    if (value != null && !"".equals(value)) {
                        // 获取字段名
                        String columnName = StringUtils.camelToUnderscore(field.getName());
                        spliceQueryField(query.value(), columnName, value, queryWrapper);

                    }
                }catch (IllegalAccessException e) {
                    log.error("条件构造器出现异常: {}", e.getMessage());
                }
            }
        }
    }

    /**
     * 拼接查询条件
     * @param queryType 注解值（条件类型）
     * @param columnName 字段名
     * @param value 值
     * @param queryWrapper 条件构造器
     * @param <T>
     */
    private static <T> void spliceQueryField(String queryType, String columnName, Object value, QueryWrapper<T> queryWrapper) {
        switch (queryType.toUpperCase()){
            case QueryConstants.QUERY_EQ:
                queryWrapper.eq(columnName,value);
                break;
            case QueryConstants.QUERY_NE:
                queryWrapper.ne(columnName,value);
                break;
            case QueryConstants.QUERY_GT:
                queryWrapper.gt(columnName,value);
                break;
            case QueryConstants.QUERY_LT:
                queryWrapper.lt(columnName,value);
                break;
            case QueryConstants.QUERY_GE:
                queryWrapper.ge(columnName,value);
                break;
            case QueryConstants.QUERY_LE:
                queryWrapper.le(columnName,value);
                break;
            case QueryConstants.QUERY_LIKE:
                queryWrapper.like(columnName,value);
                break;
            case QueryConstants.QUERY_BETWEEN:
                queryWrapper.between(columnName,value,value);
                break;
            default:
                queryWrapper.eq(columnName,value);
        }
    }

}
